package com.huidu.bitrade.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.huidu.bitrade.config.StarChargeConfig;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

@Getter
@AllArgsConstructor
public enum StarChargeEnum {

    CHARGE_ONE(1,BigDecimal.valueOf(0.40), Arrays.asList(2,1),"二星、一星"),
    CHARGE_TWO(2,BigDecimal.valueOf(0.30),Arrays.asList(5,4,3),"五星、四星、三星"),
    CHARGE_THREE(3,BigDecimal.valueOf(0.30),Arrays.asList(7,6),"七星、六星");

    public static void update(StarChargeConfig starChargeConfig){
        Integer code = starChargeConfig.getCode();
        switch (code){
            case 1:
                update(CHARGE_ONE,starChargeConfig);
                break;
            case 2:
                update(CHARGE_TWO,starChargeConfig);
                break;
            case 3:
                update(CHARGE_THREE,starChargeConfig);
                break;
        }
    }

    private static void update(StarChargeEnum starChargeEnum,StarChargeConfig starChargeConfig){
        starChargeEnum.ratio = starChargeConfig.getRatio();
        starChargeEnum.levelName = starChargeConfig.getLevelName();
        starChargeEnum.level = starChargeConfig.getLevel();
    }

    public static StarChargeEnum getByCode(Integer code){
        Optional<StarChargeEnum> any;
        any = Arrays.stream(StarChargeEnum.class.getEnumConstants())
                .filter(e -> e.getCode().equals(code)).findAny();
        return any.orElse(null);
    }

    public static StarChargeEnum getByOneLevel(Integer level){
        Optional<StarChargeEnum> any;
        any = Arrays.stream(StarChargeEnum.class.getEnumConstants())
                .filter(e -> e.getLevel().contains(level)).findAny();
        return any.orElse(null);
    }


    @EnumValue
    private Integer code;
    /**
     * 奖励比例
     */
    private BigDecimal ratio;

    /**
     * 对应等级集合
     */
    private List<Integer> level;

    /**
     * 等级名称
     */
    private String levelName;




}
